use [family-annal]
GO
-- Drop trigger in table tb_fa_files
IF OBJECT_ID ('tb_fa_files.trigger_fa_files_insert_update','TR') IS NOT NULL
   DROP TRIGGER tb_fa_files.trigger_fa_files_insert_update;
GO
-- Drop table and all constraint if exists
IF  EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[tb_fa_files]') AND name = N'pk_tb_fa_files')
BEGIN
ALTER TABLE [dbo].[tb_fa_files] DROP CONSTRAINT [pk_tb_fa_files]
END
GO
USE [family-annal]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tb_fa_files]') AND type in (N'U'))
DROP TABLE [dbo].[tb_fa_files]
GO
use [family-annal]
GO
-- create table files
create table tb_fa_files
(
	files_id int identity(1,1) not null,
	file_info nvarchar(max),
	file_code varchar(max),
	create_date datetime,
	create_uid int,
	update_date datetime,
	update_uid int,
	file_status varchar(1) default 'Y'
);
-- create constraint primary key
alter table tb_fa_files
		add constraint pk_tb_fa_files primary key (files_id);
GO
use [family-annal];
GO
-- create trigger for create_date
CREATE TRIGGER trigger_fa_files_insert_update
   ON  tb_fa_files 
   AFTER INSERT, UPDATE
AS 
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for trigger here
    IF EXISTS(SELECT * FROM INSERTED)
    BEGIN
		update tb_fa_files set create_date = GETDATE()
		from tb_fa_files t join inserted i
		on t.files_id = i.files_id;
    END
    IF NOT EXISTS(SELECT * FROM UPDATED)
    BEGIN
		update tb_fa_files set update_date = GETDATE()
		from tb_fa_files t join updated u
		on t.files_id = u.files_id;
    END
END
GO